package org.summerframework.component.upload.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.summerframework.component.mybatisplus.AbstractBasePlusService;
import org.summerframework.component.upload.file.model.dao.FileDAO;
import org.summerframework.component.upload.file.model.domain.FileDO;
import org.summerframework.component.upload.file.service.FileService;
import org.summerframework.component.upload.file.service.dto.FileDTO;
import org.summerframework.component.upload.file.service.dto.FileQueryDTO;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service("fileService")
public class FileServiceImpl extends AbstractBasePlusService<FileDAO, FileDTO, FileQueryDTO, FileDO> implements FileService {

    @Override
    public Wrapper<FileDO> getWrapper(FileQueryDTO query) {
        return Wrappers.<FileDO>lambdaQuery()
                .eq(Objects.nonNull(query.getSize()), FileDO::getSize, query.getSize())
                .orderBy(Objects.nonNull(query.getOrderByCreateTime()), query.getOrderByCreateTime() != null ? query.getOrderByCreateTime() : false, FileDO::getCreateTime)
                .orderBy(Objects.nonNull(query.getOrderById()), query.getOrderById() != null ? query.getOrderById() : false, FileDO::getId)
                .eq(StringUtils.isNotBlank(query.getType()), FileDO::getType, query.getType())
                .eq(StringUtils.isNotBlank(query.getUrl()), FileDO::getUrl, query.getUrl())
                .eq(StringUtils.isNotBlank(query.getMd5()), FileDO::getMd5, query.getMd5());
    }

    @Override
    public FileDTO selectByMd5(String md5) {
        return selectOne(FileQueryDTO.builder().md5(md5).orderByCreateTime(false).build());
    }

    @Override
    public FileDTO selectByUrl(String url) {
        return selectOne(FileQueryDTO.builder().url(url).orderByCreateTime(false).build());
    }
}
